<?php

class cronGoTask extends sfBaseTask
{
  protected function configure()
  {
    $this->addArguments(array(
        new sfCommandArgument('task_id', sfCommandArgument::OPTIONAL, 'Specific task ID to execute'),
    ));

    $this->addOptions(array(
      new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
      new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
      new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'doctrine'),
    ));

    $this->namespace = 'cron';
    $this->name = 'go';
    $this->briefDescription = 'Starts the cron manager.  Execute each minute.';
    $this->detailedDescription = <<<EOF
The [cron:go|INFO] task runs all active cron tasks defined in the Cron Manager.
Call it with:

  [php symfony cron:go [task_id]|INFO]

This file should be called by your system's crontab file each minute by adding the following:

  [* * * * * php /path/to/project/symfony cron:go|INFO]

This will run every minute and initiate all cron jobs scheduled to run at that time.
EOF;
  }

  protected function execute($arguments = array(), $options = array())
  {
    // initialize the database connection
    $databaseManager = new sfDatabaseManager($this->configuration);
    $connection = $databaseManager->getDatabase($options['connection'])->getConnection();

    $cron = new CronManagerDoctrine();

    if(empty($arguments['task_id']))
    {
      $cron->execute();
    }
    else
    {
      $cron->runProcessOnce($arguments['task_id']);
    }
  }

}
